package com.atguigu.controller;


import com.atguigu.client.SearchFeignClient;
import com.atguigu.entity.ProductImage;
import com.atguigu.entity.ProductSalePropertyKey;
import com.atguigu.entity.ProductSalePropertyValue;
import com.atguigu.entity.SkuInfo;
import com.atguigu.result.RetVal;
import com.atguigu.service.ProductImageService;
import com.atguigu.service.ProductSalePropertyKeyService;
import com.atguigu.service.ProductSalePropertyValueService;
import com.atguigu.service.SkuInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 库存单元表 前端控制器
 * </p>
 *
 * @author zhangqiang
 * @since 2021-08-25
 */
@RestController
@RequestMapping("/product")
public class SkuInfoController {

    @Autowired
    ProductSalePropertyKeyService productSalePropertyKeyService;
    @Autowired
    ProductImageService productImageService;
    @Autowired
    SkuInfoService skuInfoService;
    @Autowired
    SearchFeignClient searchFeignClient;


    //1.根据spu的id查询销售属性(包含key和value)
    @GetMapping("querySalePropertyByProductId/{productId}")
    public RetVal querySalePropertyByProductId(@PathVariable Long productId){
        List<ProductSalePropertyKey> SalePropertyKeys= productSalePropertyKeyService.querySalePropertyByProductId(productId);
        return RetVal.ok(SalePropertyKeys);
    }

    //2.根据spu的id查询spu的图片(父)信息
    @GetMapping("queryProductImageByProductId/{productId}")
    public RetVal queryProductImageByProductId(@PathVariable Long productId){
        QueryWrapper<ProductImage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("product_id",productId );
        List<ProductImage> list = productImageService.list(queryWrapper);
        return RetVal.ok(list);
    }

    //3.保存单个sku的信息
    @PostMapping("saveSkuInfo")
    public RetVal saveSkuInfo(@RequestBody SkuInfo skuInfo){
        skuInfoService.saveSkuInfo(skuInfo);
        return RetVal.ok();
    }

    //querySkuInfoByPage/1/10.分页查询sku信息
    @GetMapping("querySkuInfoByPage/{currentPageNum}/{pageSize}")
    public RetVal querySkuInfoByPage(@PathVariable Long currentPageNum,
                                     @PathVariable Long pageSize){
        Page<SkuInfo> skuInfoPage = new Page<>(currentPageNum, pageSize);
        QueryWrapper<SkuInfo> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("id");
        skuInfoService.page(skuInfoPage,queryWrapper);
        return RetVal.ok(skuInfoPage);
    }

    //    onSale/41上架
    @GetMapping("onSale/{skuId}")
    public RetVal onSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //是否销售（1：是 0：否）
        skuInfo.setIsSale(1);
        skuInfoService.updateById(skuInfo);
        searchFeignClient.onSale(skuId);
        return RetVal.ok();
    }

    //    offSale/36下架
    @GetMapping("offSale/{skuId}")
    public RetVal offSale(@PathVariable Long skuId){
        SkuInfo skuInfo = new SkuInfo();
        skuInfo.setId(skuId);
        //是否销售（1：是 0：否）
        skuInfo.setIsSale(0);
        skuInfoService.updateById(skuInfo);
        searchFeignClient.offSale(skuId);
        return RetVal.ok();
    }

}